package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.PacketInfoDto;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/10/30.
 */
public class PacketInfoRepositoryImpl {

    @PersistenceContext
    private EntityManager em;


    public List<PacketInfoDto> getDeviceRecord4DT(DTRequestDto dtRequestDto, String deviceId, String infoType) throws Exception {
        String sql = "select packetInfoId,ciphertext,timeStampCode,deviceId,infoType,portSequence," +
                "stateSequence,meterValueSequence,ipAddressFrom,portFrom,ipAddressTo,portTo,createTime,RuleSN " +
                "from tb_packet_info where  createTime like concat(:search,'%') and deviceId=:deviceId and " +
                "infoType like concat(:infoType,'%') ORDER BY CreateTime DESC";
        Query query = em.createNativeQuery(sql);
        query.setParameter("search", dtRequestDto.getSearch());
        query.setParameter("deviceId", deviceId);
        query.setParameter("infoType", infoType);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(PacketInfoDto.class));

        return query.setFirstResult(dtRequestDto.getStart()).setMaxResults(dtRequestDto.getLength()).getResultList();
    }
}
